package com.ruoyi.jjyh.controller;

import java.util.List;

import com.ruoyi.jjyh.domain.JjyhOrder;
import com.ruoyi.jjyh.domain.JjyhOrderGoods;
import com.ruoyi.jjyh.service.IJjyhOrderGoodsService;
import com.ruoyi.jjyh.service.IJjyhOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 易货订单Controller
 * 
 * @author ruoyi
 * @date 2020-11-25
 */
@Controller
@RequestMapping("/jjyh/order")
public class JjyhOrderController extends BaseController
{
    private String prefix = "jjyh/order";

    @Autowired
    private IJjyhOrderService jjyhOrderService;
    @Autowired
    private IJjyhOrderGoodsService jjyhOrderGoodsService;

    @RequiresPermissions("jjyh:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    /**
     * 查询易货订单列表
     */
    @RequiresPermissions("jjyh:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JjyhOrder jjyhOrder)
    {
        startPage();
        List<JjyhOrder> list = jjyhOrderService.selectJjyhOrderList(jjyhOrder);
        return getDataTable(list);
    }

    /**
     * 导出易货订单列表
     */
    @RequiresPermissions("jjyh:order:export")
    @Log(title = "易货订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JjyhOrder jjyhOrder)
    {
        List<JjyhOrder> list = jjyhOrderService.selectJjyhOrderList(jjyhOrder);
        ExcelUtil<JjyhOrder> util = new ExcelUtil<JjyhOrder>(JjyhOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 新增易货订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存易货订单
     */
    @RequiresPermissions("jjyh:order:add")
    @Log(title = "易货订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JjyhOrder jjyhOrder)
    {
        return toAjax(jjyhOrderService.insertJjyhOrder(jjyhOrder));
    }

    /**
     * 修改易货订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JjyhOrder jjyhOrder = jjyhOrderService.selectJjyhOrderById(id);
        mmap.put("jjyhOrder", jjyhOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存易货订单
     */
    @RequiresPermissions("jjyh:order:edit")
    @Log(title = "易货订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JjyhOrder jjyhOrder)
    {
        return toAjax(jjyhOrderService.updateJjyhOrder(jjyhOrder));
    }

    /**
     * 删除易货订单
     */
    @RequiresPermissions("jjyh:order:remove")
    @Log(title = "易货订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jjyhOrderService.deleteJjyhOrderByIds(ids));
    }

    /**
     * 待退保证金订单
     */
    @GetMapping("/rebackOrder")
    public String rebackOrder()
    {
        return prefix + "/rebackOrder";
    }

    /**
     * 查询子列表
     */
    @RequiresPermissions("erp:so:list")
    @PostMapping("/listChild")
    @ResponseBody
    public TableDataInfo listChild(long orderId)
    {
        JjyhOrderGoods jjyhOrderGoods = new JjyhOrderGoods();
        jjyhOrderGoods.setOrderId(String.valueOf(orderId));
        List<JjyhOrderGoods> jjyhOrderGoodsList = jjyhOrderGoodsService.selectJjyhOrderGoodsList(jjyhOrderGoods);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(jjyhOrderGoodsList);
        return rspData;
    }

    /**
     * 查看易货订单
     */
    @GetMapping("/viewOrder/{id}")
    public String viewOrder(@PathVariable("id") String id, ModelMap mmap)
    {
        JjyhOrder jjyhOrder = jjyhOrderService.selectPcJjyhOrderGoodsById(id);
        mmap.put("jjyhOrder", jjyhOrder);
        return prefix + "/viewOrder";
    }

    /**
     * 退回换入用户保证金
     */
    @RequiresPermissions("jjyh:order:edit")
    @Log(title = "退回换入用户保证金", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewA")
    @ResponseBody
    public AjaxResult reviewA(String id)
    {
        JjyhOrder jjyhOrder = jjyhOrderService.selectJjyhOrderById(id);
        jjyhOrder.setChangeInstatus("-1");
        return toAjax(jjyhOrderService.updateJjyhOrder(jjyhOrder));
    }

    /**
     * 退回换出用户保证金
     */
    @RequiresPermissions("jjyh:order:edit")
    @Log(title = "退回换出用户保证金", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewB")
    @ResponseBody
    public AjaxResult reviewB(String id)
    {
        JjyhOrder jjyhOrder = jjyhOrderService.selectJjyhOrderById(id);
        jjyhOrder.setChangeOutstatus("-1");
        return toAjax(jjyhOrderService.updateJjyhOrder(jjyhOrder));
    }


    /**
     * 退还保证金订单
     */
    @RequiresPermissions("jjyh:order:list")
    @PostMapping("/listReback")
    @ResponseBody
    public TableDataInfo listReback(JjyhOrder jjyhOrder)
    {
        startPage();
        jjyhOrder.setChangeInstatus("1");
        jjyhOrder.setChangeOutstatus("1");
        jjyhOrder.setOrderInstatus("4");
        jjyhOrder.setOrderOutstatus("4");
        List<JjyhOrder> list = jjyhOrderService.selectJjyhOrderList(jjyhOrder);
        return getDataTable(list);
    }
}
